package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.travel.dao.ICheckInDinnerDao;
import com.eling.elcms.travel.model.CheckInDinner;
import com.eling.elcms.travel.model.CheckInImplement.CheckStatus;
import com.eling.elcms.travel.model.CheckInImplement.DataSource;
import com.eling.elcms.travel.service.ICheckInDinnerManager;
import com.eling.elcms.travel.service.ICheckInImplementManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CheckInDinnerManagerImpl extends GenericManagerImpl<CheckInDinner, Long> implements ICheckInDinnerManager{

	ICheckInDinnerDao dinnerDao;

	@Autowired
	private ICheckInImplementManager cIImplementM;
	
	@Autowired
	public void setDao(ICheckInDinnerDao dao) {
        this.dao = dao;
        this.dinnerDao = dao;
    }
	
	@Override
	public CheckInDinner confirm(CheckInDinner dinner){
		boolean breakFast= true;
		
		if(dinner.getBreakfast() != null){
			if(dinner.getBreakfast() && dinner.getBreakfastConfirm() == null){
				breakFast =false;
			}
		}
		
		boolean lunch= true;
		if(dinner.getLunch() != null ){
			if(dinner.getLunch() && dinner.getLunchConfirm() == null){
				lunch =false;
			}
		}
	
		boolean supper= true;
		if(dinner.getSupper() != null){
			if(dinner.getSupper() && dinner.getSupperConfirm() == null){
				supper =false;
			}
		}
		
		if (breakFast && lunch && supper){
			dinner.setStatus(CheckStatus.Pended);
		}
		dinner = save(dinner);
		
		// 设置入住准备落实单总表的已落实状态
		if (dinner.getStatus() != null && dinner.getStatus() == CheckStatus.Pended){
			cIImplementM.setCiiCheckStatusConfirm(dinner.getCheckInImplement());
		}
		
		return dinner;
	}

	@Override
	public List<CheckInDinner> queryAll(CheckInDinner dinner, Date startDate, Date endDate) {
		List<CheckInDinner> returnList = new ArrayList<>();
		
		//查询机构、联盟团的入住准备车辆落实单
		PropertyUtils.setProperty(dinner, "checkInImplement.groupReserve.checkInDate", startDate);
		PropertyUtils.setProperty(dinner, "checkInImplement.groupReserve.checkInDateEnd", endDate);
		List<CheckInDinner> cidOneList = dinnerDao.query(dinner);
		if(!cidOneList.isEmpty()){
			returnList.addAll(cidOneList);
		}
		
		//查询散客团的入住准备车辆落实单
		PropertyUtils.setProperty(dinner, "checkInImplement.groupReserve", null);
		PropertyUtils.setProperty(dinner, "checkInImplement.nonMemGroupReserve.nonMemberGroup.checkInDate", startDate);
		PropertyUtils.setProperty(dinner, "checkInImplement.nonMemGroupReserve.nonMemberGroup.checkInDateEnd", endDate);
		List<CheckInDinner> cidTwoList = dinnerDao.query(dinner);
		if(!cidTwoList.isEmpty()){
			returnList.addAll(cidTwoList);
		}
		
		Collections.sort(returnList,new Comparator<CheckInDinner>() {
			public int compare(CheckInDinner o1, CheckInDinner o2) {
				Date date1 = null;
				Date date2 = null;
				//散客
				if(o1.getCheckInImplement().getDataSource()==DataSource.NonMember){
					date1 = o1.getCheckInImplement().getNonMemGroupReserve().getNonMemberGroup().getCheckInDate();
				}else{//老人团
					date1 = o1.getCheckInImplement().getGroupReserve().getCheckInDate();
				}
				//散客
				if(o2.getCheckInImplement().getDataSource()==DataSource.NonMember){
					date2 = o2.getCheckInImplement().getNonMemGroupReserve().getNonMemberGroup().getCheckInDate();
				}else{//老人团
					date2 = o2.getCheckInImplement().getGroupReserve().getCheckInDate();
				}
				return date1.compareTo(date2);
			};
		});
		
		return returnList;
	}
}
